<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="品类" prop="gameId">
        <el-select v-model="queryParams.gameId" placeholder="请选择品类" >
          <el-option v-for="item in teamCategoryOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="平台大区" prop="regionId">
        <el-select v-model="queryParams.regionId" placeholder="请选择品类" >
          <el-option v-for="item in teamCategoryTagOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-input
          v-model="queryParams.paymentMethod"
          placeholder="请输入支付方式"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="下单渠道" prop="orderChannel">
        <el-input
          v-model="queryParams.orderChannel"
          placeholder="请输入下单渠道"
          clearable
          size="small"
        />
      </el-form-item>
      <!-- <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.payTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择支付时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <p>
      <el-radio-group v-model="queryParams.itemType" @change="getList">
        <el-radio-button label="">全部订单</el-radio-button>
        <el-radio-button :label="1">进行中</el-radio-button>
        <el-radio-button :label="2">已完成</el-radio-button>
        <el-radio-button :label="3">已取消</el-radio-button>
      </el-radio-group>
    </p>


    <el-table v-loading="loading" :data="userOrderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->

      <el-table-column label="订单编号" align="center" prop="code" />
      <el-table-column label="下单用户" align="center" prop="bookingName" >
       <template slot-scope="scope">
          <span>{{ scope.row.bookingName +"("+ scope.row.bookingCode + ")" }}</span>
        </template>
      </el-table-column>

      <el-table-column label="品类" align="center" prop="gameName" />
      <el-table-column label="大区" align="center" prop="regionName" />
      <el-table-column label="接单大神" align="center" prop="accompanyName" >
       <template slot-scope="scope">
          <span>{{ scope.row.accompanyName +"("+ scope.row.accompanyCode + ")" }}</span>
        </template>
      </el-table-column>

      <el-table-column label="数量" align="center" prop="number">
        <template slot-scope="scope">
          <span>{{ scope.row.number +"("+ scope.row.unit + ")" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠券" align="center"   prop="voucherCardAmount" />
      <el-table-column label="差价猫粮" align="center" prop="differencePrice" />
      <el-table-column label="实付猫粮" align="center" prop="paidPrice" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-if="scope.row.status==1">进行中</span>
          <span v-else-if="scope.row.status==2">已完成</span>
          <span v-else-if="scope.row.status==3">已取消</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="paymentMethod" />
      <el-table-column label="下单渠道" align="center" prop="orderChannel" />
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.itemType === 2" label="完成时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.itemType === 3" label="取消时间" align="center" prop="cancelTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cancelTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.itemType === 3" label="取消人" align="center" prop="cancelBy" />
      <el-table-column v-if="queryParams.itemType === 3" label="取消原因" align="center" prop="cancelReason" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listUserOrder, getUserOrder, delUserOrder, addUserOrder, updateUserOrder, exportUserOrder } from "@/api/system/userOrder";
import { loadTeamCategoryItem } from "@/api/system/teamCategoryItem";
import { loadTeamCategoryTag} from "@/api/system/teamCategoryTag";


export default {
  name: "UserOrder",
  data() {
    return {
      teamCategoryOptions:[],
      teamCategoryTagOptions:[],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户订单表格数据
      userOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gameId: undefined,
        bookingId: undefined,
        accompanyId: undefined,
        number: undefined,
        score: undefined,
        voucherCardId: undefined,
        regionId: undefined,
        totalPrice: undefined,
        differencePrice: undefined,
        paidPrice: undefined,
        status: undefined,
        paymentMethod: undefined,
        orderChannel: undefined,
        payTime: undefined,
        orderTime: undefined,
        orderIncome: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "状态(1进行中，2已完成，3已取消)不能为空", trigger: "blur" }
        ],
        orderIncome: [
          { required: true, message: "订单收益不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

    loadTeamCategoryItem().then(response => {
      this.teamCategoryOptions = response.data;
    });
    loadTeamCategoryTag().then(response => {
      this.teamCategoryTagOptions = response.data;
    });
  },
  methods: {
    /** 查询用户订单列表 */
    getList() {
      this.loading = true;
      listUserOrder(this.queryParams).then(response => {
        this.userOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gameId: undefined,
        bookingId: undefined,
        accompanyId: undefined,
        number: undefined,
        score: undefined,
        createTime: undefined,
        voucherCardId: undefined,
        regionId: undefined,
        totalPrice: undefined,
        differencePrice: undefined,
        paidPrice: undefined,
        status: "0",
        paymentMethod: undefined,
        orderChannel: undefined,
        payTime: undefined,
        orderTime: undefined,
        orderIncome: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户订单";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUserOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUserOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户订单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>